package com.cleanvegetable.web.controller;

import com.cleanvegetable.common.result.Result;
import com.cleanvegetable.service.dingtalk.DingTalkService;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse.DeptBaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 钉钉控制器
 * 处理钉钉相关的请求
 *
 * @author cleanvegetable
 * @date 2024/02/24
 */
@Slf4j
@RestController
@RequestMapping("/api/dingtalk")
public class DingTalkController {
    
    /**
     * 钉钉服务
     */
    @Autowired
    private DingTalkService dingTalkService;
    
    /**
     * 获取部门列表
     *
     * @param deptId 部门ID，根部门传1
     * @param language 通讯录语言：zh_CN（默认）、en_US
     * @return 部门列表
     */
    @GetMapping("/department/list")
    public Result<List<DeptBaseResponse>> getDepartmentList(
            @RequestParam(defaultValue = "1") Long deptId,
            @RequestParam(defaultValue = "zh_CN") String language) {
        log.info("获取部门列表，部门ID：{}，语言：{}", deptId, language);
        List<DeptBaseResponse> departments = dingTalkService.getDepartmentList(deptId, language);
        log.info("获取部门列表成功，数量：{}", departments.size());
        return Result.success(departments);
    }
    
    /**
     * 发送工作通知
     *
     * @param userIds 用户ID列表，多个用逗号分隔
     * @param content 消息内容
     * @return 发送结果
     */
    @GetMapping("/notification/send")
    public Result<Void> sendWorkNotification(
            @RequestParam String userIds,
            @RequestParam String content) {
        log.info("发送工作通知，用户ID：{}，内容：{}", userIds, content);
        String[] userIdArray = userIds.split(",");
        dingTalkService.sendWorkNotification(userIdArray, content);
        log.info("发送工作通知成功");
        return Result.success(null);
    }
    
    /**
     * 发送群机器人消息
     *
     * @param content 消息内容
     * @return 发送结果
     */
    @GetMapping("/robot/send")
    public Result<Void> sendRobotMessage(@RequestParam String content) {
        log.info("发送群机器人消息，内容：{}", content);
        dingTalkService.sendRobotMessage(content);
        log.info("发送群机器人消息成功");
        return Result.success(null);
    }
} 